package id.co.rho.scoring.cid.shared.model;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import id.co.rho.scoring.cid.changecontrol.entitychange.UpdateEntityChange;
import id.co.rho.scoring.cid.client.model.ChangeTrackingModel;

public class UserModel extends ChangeTrackingModel{
	private static final long serialVersionUID = 1L;
	public static final String ID = "id";
	public static final String USER_ID = "userId"; 
	public static final String USER_NAME = "userName";
	public static final String USER_PASS = "userPass";
	public static final String STATUS = "userStatus";
	public static final String EMAIL = "email";
	public static final String FUNCTIONGROUPS = "functionGroups";
	
	private List<FunctionGroupModel> functionGroupModelList;
	
	public UserModel() {
		super();
	}
	
	public UserModel(UpdateEntityChange change) {
		super(change);
	}
	
	public static List<UserModel> applyChanges(List<UserModel> modelList, List<UpdateEntityChange> uecList, List<FunctionGroupModel> allFunctionGroupList){
    	Long idModel, idUec;
    	UserModel newModel;
    	if(uecList!=null){
	    	for (UpdateEntityChange change : uecList) {
				idUec = (Long) change.getEntityId();
				if(idUec!=null){
			    	for (UserModel model : modelList) {
			    		idModel = (Long) model.get(UserModel.ID);    		
		    			if(idModel.compareTo(idUec)==0){
		    				model.setChange(change);
		    				if(change.getRecordedChange(FUNCTIONGROUPS)!=null){
								Set idFunctionGroups = (Set) change.getRecordedChange(FUNCTIONGROUPS).getNewValue();
								model = applyChangesSet(model,idFunctionGroups,allFunctionGroupList);
							}
		    			}
		    		}
				}else{
					newModel = new UserModel(change);
					if(change.getRecordedChange(FUNCTIONGROUPS)!=null){
						Set idFunctionGroups = (Set) change.getRecordedChange(FUNCTIONGROUPS).getNewValue();
						newModel = applyChangesSet(newModel,idFunctionGroups,allFunctionGroupList);
					}
					modelList.add(newModel);
				}
	    	}
    	}
    	return modelList;
    }
	
	public static UserModel applyChangesSet(UserModel usr,Set idFunctionGroup, List<FunctionGroupModel> allFunctionGroupList){
		usr.setFunctionGroupModelList(fromSetToList(allFunctionGroupList, idFunctionGroup));
		return usr;
	}

	public List<FunctionGroupModel> getFunctionGroupModelList() {
		return functionGroupModelList;
	}

	public void setFunctionGroupModelList(List<FunctionGroupModel> functionGroupModelList) {
		this.functionGroupModelList = functionGroupModelList;
	}
	
	public void updateFunctionList(List<FunctionGroupModel> newFunctionGroupModelList) {
		set(FUNCTIONGROUPS, convertToIdSet(newFunctionGroupModelList));
		this.functionGroupModelList = newFunctionGroupModelList;
	}
	
	public Set<Long> convertToIdSet(List<FunctionGroupModel> list){
		Set<Long> set = new HashSet();
		if(list!=null){
			for(FunctionGroupModel fg: list){
				set.add((Long) fg.get(FunctionGroupModel.ID));
			}
		}
		return set;
	}
	
	public static List<FunctionGroupModel> fromSetToList(List<FunctionGroupModel> allList, Set<Long> idSet){
		List<FunctionGroupModel> newList = new ArrayList();
		FunctionGroupModel mod = null;
		for(Long id : idSet){
			mod = getFunctionFromListById(allList, id);
			if(mod!=null){
				newList.add(mod);
			}
		}
		return newList;
	}
	
	public static FunctionGroupModel getFunctionFromListById(List<FunctionGroupModel> allList, Long id){
		FunctionGroupModel mod = null;
		for(FunctionGroupModel model : allList){
			if(id.compareTo((Long) model.get(FunctionGroupModel.ID))==0){
				mod = model;
			}
		}
		return mod;
	}
	
}
